/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package victoralbertos.io.ohos.slice;

import io.rx_cache.Reply;
import io.rx_cache.entities.User;
import io.rx_cache.entities.UserWithSource;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import rx.Subscriber;
import rx.schedulers.Schedulers;
import victoralbertos.io.ohos.ResourceTable;
import ohos.aafwk.content.Intent;
import victoralbertos.io.ohos.net.OhosSchedulers;
import victoralbertos.io.ohos.provider.ListProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页功能
 *
 * @author name
 * @since 2021-06-03
 */
public class MainAbilitySlice extends BaseAbilitySlice {
    private ListProvider listLeftProvider;
    private ListProvider listRightProvider;
    private List<UserWithSource> users = new ArrayList<>();
    private List<UserWithSource> datas = new ArrayList<>();
    private ListContainer listLeft;
    private ListContainer listRight;

    private Button refresh;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        createView();
    }

    /**
     * 初始化列表
     */
    private void createView() {
        listLeft = (ListContainer) findComponentById(ResourceTable.Id_list_left);
        listRight = (ListContainer) findComponentById(ResourceTable.Id_list_right);
        refresh = (Button) findComponentById(ResourceTable.Id_refresh);
        refresh.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getSecData(true);
                getMinData(true);
            }
        });
        listLeftProvider = new ListProvider(new ArrayList<>(), this);
        listRightProvider = new ListProvider(new ArrayList<>(), this);
        listLeft.setItemProvider(listLeftProvider);
        listRight.setItemProvider(listRightProvider);
        getSecData(false);
        getMinData(false);
    }

    private void getSecData(boolean isRefresh) {
        int lastUserId = 1;
        if (!users.isEmpty()) {
            lastUserId = users.get(users.size() - 1).getUser().getId();
        }
        if (isRefresh) {
            lastUserId = 1;
        }

        subscription.unsubscribe();
        subscription = getRepository().getUsers(lastUserId, isRefresh)
                .subscribeOn(Schedulers.newThread())
                .observeOn(OhosSchedulers.mainThread())
                .subscribe(new Subscriber<Reply<List<User>>>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(Reply<List<User>> reply) {
                        if (isRefresh) {
                            users.clear();
                        }
                        for (User user : reply.getData()) {
                            users.add(new UserWithSource(user, reply.getSource().name()));
                        }
                        listLeftProvider.setData(users);
                        listLeftProvider.notifyDataChanged();
                    }
                });
    }

    private void getMinData(boolean isRefresh) {
        int lastUserId = 1;
        if (!datas.isEmpty()) {
            lastUserId = datas.get(datas.size() - 1).getUser().getId();
        }
        if (isRefresh) {
            lastUserId = 1;
        }

        subscription = getRepository().getLockUsers(lastUserId, isRefresh)
                .subscribeOn(Schedulers.newThread())
                .observeOn(OhosSchedulers.mainThread())
                .subscribe(new Subscriber<Reply<List<User>>>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(Reply<List<User>> reply) {
                        if (isRefresh) {
                            datas.clear();
                        }

                        for (User user : reply.getData()) {
                            datas.add(new UserWithSource(user, reply.getSource().name()));
                        }
                        listRightProvider.setData(datas);
                        listRightProvider.notifyDataChanged();
                    }
                });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
